<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class b2c_ctl_admin_member_shenhe extends desktop_controller{

    var $workground = 'b2c_ctl_admin_member';
    var $member_model ;

    public function __construct($app)
    {
        parent::__construct($app);
        $this->member_model = $this->app->model('member_shenhe');
        header("cache-control: no-store, no-cache, must-revalidate");
        $user = kernel::single('desktop_user');
        $this->username = $user->user_data['account']['login_name'];
        $this->user_id = $user->user_data['user_id'];
        $this->is_super = $user->user_data['super'];
    }

    function index(){

        $custom_actions[] =  array('label'=>app::get('b2c')->_('审核通过'),'submit'=>'index.php?app=b2c&ctl=admin_member_shenhe&act=allow');
        $custom_actions[] =  array('label'=>app::get('b2c')->_('审核拒绝'),'submit'=>'index.php?app=b2c&ctl=admin_member_shenhe&act=refuse');

        $actions_base['title'] = app::get('b2c')->_('预存款审核');
        if($this->is_super){
            $actions_base['base_filter'] = array('disabled' => 'false');
        }else{
            $store_id = $this->find_storeid_user($this->user_id);
            $actions_base['base_filter'] = array('disabled' => 'false','store_id'=>$store_id);
        }
        $actions_base['actions'] = $custom_actions;
        $actions_base['use_buildin_recycle'] = false;
        $this->finder('b2c_mdl_member_shenhe',$actions_base);
    }

    public function find_storeid_user($user_id){
        $hasroleObj = app::get('desktop')->model('hasrole');
        $rolesObj = app::get('desktop')->model('roles');
        $role_id = $hasroleObj->getList('role_id',array('user_id'=>$user_id));
        $store_id = $rolesObj->getList('store_id',array('role_id'=>$role_id[0]['role_id']));
        return $store_id[0]['store_id'];
    }
     /**
     * 桌面订单详细汇总显示
     * @param null
     * @return null
     */
    public function _views(){
        $obj_shenhe = $this->app->model('member_shenhe');
        if($this->is_super){
            $sub_menu = array(
                1=>array('label'=>app::get('b2c')->_('未审核'),'optional'=>false,'filter'=>array('is_shenhe'=>0,'disabled'=>'false')),
                2=>array('label'=>app::get('b2c')->_('审核完成'),'optional'=>false,'filter'=>array('is_shenhe'=>1,'disabled'=>'false')),
                3=>array('label'=>app::get('b2c')->_('审核拒绝'),'optional'=>false,'filter'=>array('is_shenhe'=>2,'disabled'=>'false')),
                4=>array('label'=>app::get('b2c')->_('全部'),'optional'=>false,'filter'=>array('disabled'=>'false')),
            );
        }else{
            $store_id = $this->find_storeid_user($this->user_id);
            $sub_menu = array(
                1=>array('label'=>app::get('b2c')->_('未审核'),'optional'=>false,'filter'=>array('is_shenhe'=>0,'disabled'=>'false','store_id'=>$store_id)),
                2=>array('label'=>app::get('b2c')->_('审核完成'),'optional'=>false,'filter'=>array('is_shenhe'=>1,'disabled'=>'false','store_id'=>$store_id)),
                3=>array('label'=>app::get('b2c')->_('审核拒绝'),'optional'=>false,'filter'=>array('is_shenhe'=>2,'disabled'=>'false','store_id'=>$store_id)),
                4=>array('label'=>app::get('b2c')->_('全部'),'optional'=>false,'filter'=>array('disabled'=>'false','store_id'=>$store_id)),
            );
        }

        if(isset($_GET['optional_view'])) $sub_menu[$_GET['optional_view']]['optional'] = false;

        foreach($sub_menu as $k=>$v){
            if($v['optional']==false){
                $show_menu[$k] = $v;
                if(is_array($v['filter'])){
                    $v['filter'] = array_merge(array(),$v['filter']);
                }else{
                    $v['filter'] = array();
                }
                $show_menu[$k]['filter'] = $v['filter']?$v['filter']:null;
                if($k==$_GET['view']){
                    $show_menu[$k]['newcount'] = true;
                    $show_menu[$k]['addon'] = $obj_shenhe->count($v['filter']);
                }
                $show_menu[$k]['href'] = 'index.php?app=b2c&ctl=admin_member_shenhe&act=index&view='.($k).(isset($_GET['optional_view'])?'&optional_view='.$_GET['optional_view'].'&view_from=dashboard':'');
            }elseif(($_GET['view_from']=='dashboard')&&$k==$_GET['view']){
                $show_menu[$k] = $v;
            }
        }

        return $show_menu;
    }

    function allow(){
        if($_POST['log_id']){
            $db = kernel::database();
            $db->beginTransaction();
            $obj_shenhe = app::get('b2c')->model('member_shenhe');
            $adjmember = $this->app->model('members');
            $mem_adv = $this->app->model('member_advance');
            $memberZObj = app::get('b2c')->model('member_zong');
            $aShenhe = $_POST['log_id'];
            $pam_member = app::get('pam')->model('members');
            $aList = array();
            foreach( $aShenhe as $key=>$sid){
              $aList[$key] =array_values($obj_shenhe->getList('*',array('log_id'=>$sid)));
            }   
            foreach ($aList as $key1=>&$v) {
                if($v[0]['is_shenhe'] == '1' || $v[0]['is_shenhe'] == '2'){
                    continue;
                }
                $log_id=$v[0]['log_id'];
                $data[$key1] = array(
                    'member_id'=>$v[0]['member_id'],
                    'money'=>$v[0]['money'],                    
                    'message'=>$v[0]['message'],
                    'mtime'=>time(),
                    'payment_id'=>$v[0]['payment_id'],
                    'order_id'=>$v[0]['order_id'],
                    'paymethod'=>$v[0]['paymethod'],
                    'yuzhi'=>$v[0]['yuzhi'],
                    'memo'=>$v[0]['memo'],
                    'import_money'=>$v[0]['import_money'],
                    'explode_money'=>$v[0]['explode_money'],
                    'member_advance'=>$v[0]['member_advance'],
                    'shop_advance'=>$v[0]['shop_advance'],
                    'store_id'=>$v[0]['store_id'],
                );
                if($mem_adv->save($data[$key1])){
                    /** 监听预存款变化 **/
                    if($v[0]['import_money'] > 0 && $v[0]['explode_money'] == 0.00){
                        $adjmember->db->exec('UPDATE sdb_b2c_members SET advance = advance +'.$v[0]['money'].' where member_id='.$v[0]['member_id']);
                    }elseif ($v[0]['import_money'] == 0.00 && $v[0]['explode_money'] > 0) {
                        $adjmember->db->exec('UPDATE sdb_b2c_members SET advance = advance -'.$v[0]['money'].' where member_id='.$v[0]['member_id'].' and advance >='.$v[0]['money']);
                    }
                    // $obj_shenhe->db->exec('UPDATE sdb_b2c_member_shenhe SET is_shenhe = "1" where log_id = '.$log_id);
                    if(!$this->username){
                        $db->rollback();
                        $db->commit();
                        continue;
                    }
                    $filter1['log_id']=$log_id;
                    $data1['is_shenhe']='1';
                    $data1['who_shenhe']=$this->username;
                    $data1['shenhetime']=time();
                    $true = $obj_shenhe->update($data1,$filter1);
                    if(!$true){
                        $db->rollback();
                        $db->commit();
                        continue;
                    }
                    $id = $memberZObj->getList('id',array('member_id'=>$v[0]['member_id']));
                    $mobile = $pam_member->getList('login_account',array('login_type'=>'mobile','member_id'=>$member_id));
                    $name = $pam_member->getList('login_account',array('login_type'=>'local','member_id'=>$member_id));
                    if($id){
                        $data1['id']=$id[0]['id'];
                        $data1['mobile'] = $mobile[0]['login_account'];
                        $data1['name'] = $name[0]['login_account'];
                        $data1['yuzhi'] = $v[0]['yuzhi'];
                        $true = $memberZObj->save($data1);
                        if(!$true){
                            $db->rollback();
                            $db->commit();
                            continue;
                        }
                    }else{
                        $data1['mobile'] = $mobile[0]['login_account'];
                        $data1['name'] = $name[0]['login_account'];
                        $data1['member_id']=$v[0]['member_id'];
                        $data1['yuzhi'] = $v[0]['yuzhi'];
                        $true = $memberZObj->save($data1);
                        if(!$true){
                            $db->rollback();
                            $db->commit();
                            continue;
                        }
                    }
                    foreach(kernel::servicelist('member_advance_listener') as $service)
                    {
                        $arr_params = array(
                            'member_id'=>$v[0]['member_id'],
                            'doadd'=>false,
                            'is_frontend'=> false ,
                        );
                        $service->listener_advance($arr_params);
                    }
                    $db->commit();
                }else{
                    $db->commit();
                    $this->splash('error','','审核失败，写入数据库失败！');
                }
            }

            foreach ($data as &$vv) {
                if($obj_operatorlogs = kernel::service('operatorlog.members')){
                    $olddata = app::get('b2c')->model('members')->dump($vv['$member_id']);
                }
                #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓记录管理员操作日志@lujy↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
                if($obj_operatorlogs = kernel::service('operatorlog.members')){
                    if(method_exists($obj_operatorlogs,'detail_advance_log')){
                        $newdata = app::get('b2c')->model('members')->dump($vv['$member_id']);
                        $obj_operatorlogs->detail_advance_log($newdata,$olddata);
                    }
                }
            }
            $url = "?app=b2c&ctl=admin_member_shenhe&act=index";
            $this->splash('success',$url,'审核成功！');
        }
    }

    function refuse(){
        $obj_shenhe = app::get('b2c')->model('member_shenhe');
        $adjmember = $this->app->model('members');
        $mem_adv = $this->app->model('member_advance');
        $aShenhe = $_POST['log_id'];
        $shenhe_id=implode(',',$aShenhe); 
        $aList = array();
        foreach( $aShenhe as $sid){
          $aList[] =array_values($obj_shenhe->getList('*',array('log_id'=>$sid)));
        }   

        foreach ($aList as $v) {
            if($v[0]['is_shenhe'] == '1' || $v[0]['is_shenhe'] == '2' || !$this->username){
                continue;
            }
            $log_id=$v[0]['log_id'];
            // dump('UPDATE sdb_b2c_member_shenhe SET is_shenhe = 2 ,who_shenhe = '.$this->username.',shenhetime = '.time().' where log_id = '.$log_id);exit();
            // $res = $obj_shenhe->db->exec('UPDATE sdb_b2c_member_shenhe SET is_shenhe = 2 ,who_shenhe = '.$this->username.',shenhetime = '.time().' where log_id = '.$log_id);
            $data['is_shenhe'] = 2;
            $data['who_shenhe'] = $this->username;
            $data['shenhetime'] = time();
            $filter['log_id'] = $log_id;
            $obj_shenhe->update($data,$filter);
        }
         $url = "?app=b2c&ctl=admin_member_shenhe&act=index";
         $this->splash('success',$url,'拒绝审核成功！');
    }


}
